package threadlab;
public class SharedResourceExample {
    static class SharedResource {
        private int sharedValue = 0;
        public synchronized void increment() {
            sharedValue++;
            System.out.println("Incremented to: " + sharedValue);
        }
        public synchronized void printValue() {
            System.out.println("Current value: " + sharedValue);
        }
    }
    public static void main(String[] args) {
        SharedResource resource = new SharedResource();
        
        Thread incrementThread = new Thread(() -> {
            for(int i = 0; i < 5; i++) {
                resource.increment();
                try {
                    Thread.sleep(100); 
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread printThread = new Thread(() -> {
            for(int i = 0; i < 5; i++) {
                resource.printValue();
                try {
                    Thread.sleep(150); 
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        incrementThread.start();
        printThread.start();
    }
}